﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.Apps.ServiceImpl.Attendance.Repositories.Interface;
using EzDesk.Apps.Model.Attendance;

namespace EzDesk.Apps.ServiceImpl.Attendance.Repositories
{
    public partial class EvaluationModifyHistoryRepository : IEvaluationModifyHistoryRepository
    {
        #region [ private mothods] 
        private IList<EvaluationModifyHistoryChangeItem> getEvaluationModifyHistoryChangeItemList(Guid historyId)
        {
            IList<EvaluationModifyHistoryChangeItem> result = null;
            var query = _db.ez_EvaluationChangeItems
                .Where(p => p.HistoryId == historyId)
                .ToList();

            if (query.Count > 0)
            {
                result = new List<EvaluationModifyHistoryChangeItem>();
                foreach (var changeItem in query)
                {
                    result.Add(
                        new EvaluationModifyHistoryChangeItem()
                        {
                            ItemName = changeItem.ItemName,
                            OldValue = changeItem.OldValue,
                            NewValue = changeItem.NewValue,
                        });
                }
            }

            return result;
        }

        private void addEvaluationChangeItem(Guid historyId, string itemName, string oldValue, string newValue)
        {
            ez_EvaluationChangeItem changeItem = new ez_EvaluationChangeItem()
            {
                ChangeItemId = Guid.NewGuid(),
                HistoryId = historyId,
                ItemName = itemName,
                OldValue = oldValue,
                NewValue = newValue,
            };
            _db.ez_EvaluationChangeItems.InsertOnSubmit(changeItem);
        }

        private void addEvaluationChange(
            Guid historyId,
            DayAttendanceEvaluation oldAttendanceEvaluation, 
            DayAttendanceEvaluation newAttendanceEvaluation
            )
        {
            // 记录迟到时间
            if (oldAttendanceEvaluation.LateMiniutes != newAttendanceEvaluation.LateMiniutes)
            {
                addEvaluationChangeItem(
                    historyId,
                    "LateMiniutes", 
                    oldAttendanceEvaluation.LateMiniutes.ToString(), 
                    newAttendanceEvaluation.LateMiniutes.ToString()
                    );
            }
            // 记录早退时间
            if (oldAttendanceEvaluation.EarlyMiniutes != newAttendanceEvaluation.EarlyMiniutes)
            {
                addEvaluationChangeItem(
                    historyId,
                    "EarlyMiniutes",
                    oldAttendanceEvaluation.EarlyMiniutes.ToString(),
                    newAttendanceEvaluation.EarlyMiniutes.ToString()
                    );
            }
            // 记录是否缺勤
            if (oldAttendanceEvaluation.IsAbsense != newAttendanceEvaluation.IsAbsense)
            {
                addEvaluationChangeItem(
                    historyId,
                    "IsAbsense",
                    oldAttendanceEvaluation.IsAbsense.ToString(),
                    newAttendanceEvaluation.IsAbsense.ToString()
                    );
            }
            // 记是否加班
            if (oldAttendanceEvaluation.IsExtraWork != newAttendanceEvaluation.IsExtraWork)
            {
                addEvaluationChangeItem(
                    historyId,
                    "IsExtraWork",
                    oldAttendanceEvaluation.IsExtraWork.ToString(),
                    newAttendanceEvaluation.IsExtraWork.ToString()
                    );
            }
            // 记录实际工时
            if (oldAttendanceEvaluation.Manhours != newAttendanceEvaluation.Manhours)
            {
                addEvaluationChangeItem(
                    historyId,
                    "ManHours",
                    oldAttendanceEvaluation.Manhours.ToString(),
                    newAttendanceEvaluation.Manhours.ToString()
                    );
            }
        }

        #endregion


        public IList<EvaluationModifyHistory> GetModifyHistoryByUserAndDate(Guid userId, DateTime date)
        {
            IList<EvaluationModifyHistory> result = null;

            var query = GetAll()
                .Where(p => p.UserId == userId && p.Date == date)
                .OrderByDescending(p => p.ModifyOn)
                .ToList();

            if (query.Count > 0)
            {
                result = new List<EvaluationModifyHistory>();
                foreach (var modifyHistory in query)
                {
                    modifyHistory.AttendanceEvaluationChangeList = getEvaluationModifyHistoryChangeItemList(
                        modifyHistory.Id
                        );
                    result.Add(modifyHistory);
                }
            }
            return result;
        }

        public void AddModifyHistory(
            DayAttendanceEvaluation oldAttendanceEvaluation,
            DayAttendanceEvaluation newAttendanceEvaluation, 
            Guid operationId, 
            DateTime modifyOn)
        {
            Guid userId = newAttendanceEvaluation.UserId;
            Guid newHistoryId = Guid.NewGuid();

            var modifyHistory = new EvaluationModifyHistory()
            {
                Id = newHistoryId,
                UserId = userId,
                Date = newAttendanceEvaluation.Date,
                ModifyOn = modifyOn,
                OperatorId = operationId,
            };
            Add(modifyHistory, false);

            addEvaluationChange(newHistoryId, oldAttendanceEvaluation, newAttendanceEvaluation);

            SaveChanges();
        }




        
    }
}
